package com.ruoyi.wms.service.impl;

import java.util.List;
import java.math.BigDecimal;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.wms.mapper.WmsOutboundOrderDetailMapper;
import com.ruoyi.wms.domain.WmsOutboundOrderDetail;
import com.ruoyi.wms.service.IWmsOutboundOrderDetailService;

/**
 * 出库单明细Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-01-01
 */
@Service
public class WmsOutboundOrderDetailServiceImpl implements IWmsOutboundOrderDetailService 
{
    @Autowired
    private WmsOutboundOrderDetailMapper wmsOutboundOrderDetailMapper;

    /**
     * 查询出库单明细
     * 
     * @param detailId 出库单明细主键
     * @return 出库单明细
     */
    @Override
    public WmsOutboundOrderDetail selectWmsOutboundOrderDetailById(Long detailId)
    {
        return wmsOutboundOrderDetailMapper.selectWmsOutboundOrderDetailById(detailId);
    }

    /**
     * 查询出库单明细列表
     * 
     * @param wmsOutboundOrderDetail 出库单明细
     * @return 出库单明细
     */
    @Override
    public List<WmsOutboundOrderDetail> selectWmsOutboundOrderDetailList(WmsOutboundOrderDetail wmsOutboundOrderDetail)
    {
        // 设置租户ID
        wmsOutboundOrderDetail.setTenantId(SecurityUtils.getTenantId());
        return wmsOutboundOrderDetailMapper.selectWmsOutboundOrderDetailList(wmsOutboundOrderDetail);
    }

    /**
     * 根据出库单ID查询明细列表
     * 
     * @param orderId 出库单ID
     * @return 出库单明细集合
     */
    @Override
    public List<WmsOutboundOrderDetail> selectWmsOutboundOrderDetailByOrderId(Long orderId)
    {
        return wmsOutboundOrderDetailMapper.selectWmsOutboundOrderDetailByOrderId(orderId);
    }

    /**
     * 新增出库单明细
     * 
     * @param wmsOutboundOrderDetail 出库单明细
     * @return 结果
     */
    @Override
    public int insertWmsOutboundOrderDetail(WmsOutboundOrderDetail wmsOutboundOrderDetail)
    {
        // 设置租户ID和创建信息
        wmsOutboundOrderDetail.setTenantId(SecurityUtils.getTenantId());
        wmsOutboundOrderDetail.setCreateBy(SecurityUtils.getUsername());
        wmsOutboundOrderDetail.setCreateTime(DateUtils.getNowDate());
        
        // 设置默认状态和数量
        if (wmsOutboundOrderDetail.getDetailStatus() == null || wmsOutboundOrderDetail.getDetailStatus().isEmpty()) {
            wmsOutboundOrderDetail.setDetailStatus("PENDING"); // 待出库
        }
        if (wmsOutboundOrderDetail.getAllocatedQty() == null) {
            wmsOutboundOrderDetail.setAllocatedQty(BigDecimal.ZERO);
        }
        if (wmsOutboundOrderDetail.getPickedQty() == null) {
            wmsOutboundOrderDetail.setPickedQty(BigDecimal.ZERO);
        }
        if (wmsOutboundOrderDetail.getShippedQty() == null) {
            wmsOutboundOrderDetail.setShippedQty(BigDecimal.ZERO);
        }
        
        return wmsOutboundOrderDetailMapper.insertWmsOutboundOrderDetail(wmsOutboundOrderDetail);
    }

    /**
     * 修改出库单明细
     * 
     * @param wmsOutboundOrderDetail 出库单明细
     * @return 结果
     */
    @Override
    public int updateWmsOutboundOrderDetail(WmsOutboundOrderDetail wmsOutboundOrderDetail)
    {
        wmsOutboundOrderDetail.setUpdateBy(SecurityUtils.getUsername());
        wmsOutboundOrderDetail.setUpdateTime(DateUtils.getNowDate());
        return wmsOutboundOrderDetailMapper.updateWmsOutboundOrderDetail(wmsOutboundOrderDetail);
    }

    /**
     * 批量删除出库单明细
     * 
     * @param detailIds 需要删除的出库单明细主键
     * @return 结果
     */
    @Override
    public int deleteWmsOutboundOrderDetailByIds(Long[] detailIds)
    {
        return wmsOutboundOrderDetailMapper.deleteWmsOutboundOrderDetailByIds(detailIds);
    }

    /**
     * 删除出库单明细信息
     * 
     * @param detailId 出库单明细主键
     * @return 结果
     */
    @Override
    public int deleteWmsOutboundOrderDetailById(Long detailId)
    {
        return wmsOutboundOrderDetailMapper.deleteWmsOutboundOrderDetailById(detailId);
    }

    /**
     * 根据出库单ID删除明细
     * 
     * @param orderId 出库单ID
     * @return 结果
     */
    @Override
    public int deleteWmsOutboundOrderDetailByOrderId(Long orderId)
    {
        return wmsOutboundOrderDetailMapper.deleteWmsOutboundOrderDetailByOrderId(orderId);
    }

    /**
     * 批量新增出库单明细
     * 
     * @param detailList 出库单明细列表
     * @return 结果
     */
    @Override
    public int batchInsertWmsOutboundOrderDetail(List<WmsOutboundOrderDetail> detailList)
    {
        if (detailList == null || detailList.isEmpty()) {
            return 0;
        }
        
        // 设置租户ID和创建信息
        for (WmsOutboundOrderDetail detail : detailList) {
            detail.setTenantId(SecurityUtils.getTenantId());
            detail.setCreateBy(SecurityUtils.getUsername());
            detail.setCreateTime(DateUtils.getNowDate());
            
            // 设置默认状态和数量
            if (detail.getDetailStatus() == null || detail.getDetailStatus().isEmpty()) {
                detail.setDetailStatus("PENDING"); // 待出库
            }
            if (detail.getAllocatedQty() == null) {
                detail.setAllocatedQty(BigDecimal.ZERO);
            }
            if (detail.getPickedQty() == null) {
                detail.setPickedQty(BigDecimal.ZERO);
            }
            if (detail.getShippedQty() == null) {
                detail.setShippedQty(BigDecimal.ZERO);
            }
        }
        
        return wmsOutboundOrderDetailMapper.batchInsertWmsOutboundOrderDetail(detailList);
    }
}